﻿using System;
using Karma.Framework.Core.Metadata.Mapping;
using Newtonsoft.Json;

namespace Karma.Framework.Core.Modules.Metadata.Converter
{
    public class EntityMetadataConverter : JsonConverter
    {
        public override void WriteJson(JsonWriter writer, object value)
        {
            var entity = value as Entity;
            if (entity == null)
            {
                throw new ArgumentException("Value debe ser un Entity", "value");
            }

            JsonSerializer serializer = new JsonSerializer();
            serializer.Serialize(writer, entity);
        }

        public override object ReadJson(JsonReader reader, Type objectType)
        {
            throw new InvalidOperationException();
        }

        public override bool CanConvert(Type objectType)
        {
            return objectType.Equals(typeof(Entity));
        }
    }
}
